const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const passport = require('passport')
const app = express()

//引入 users.js
const users = require('./routers/api/users')
//引入 profiles.js
const profiles = require('./routers/api/profiles')

//DB config
const db = require('./config/keys').mongoURI

// use body-parser 中间件
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())



//connect to mongodb

mongoose.connect(db, {useNewUrlParser:true})
        .then(() => console.log('MongoDB Connected'))
        .catch(err => console.log(err))
mongoose.set('useFindAndModify', false);

//passport初始化
app.use(passport.initialize())
require('./config/passport')(passport)



//使用router
app.use('/api/users', users)
app.use('/api/profiles', profiles)

const port = process.env.PORT || 5000

app.listen(port, () => {
  console.log(`Server is running on port ${port}`)
})